//
//  DDanceViewController.m
//  dance
//
//  Created by aether on 2023/10/19.
//

#import "DDanceViewController.h"
#import "DInfoViewController.h"
#import "DDanceViewCell.h"

@interface DDanceViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *table;
@property (nonatomic, strong) NSArray *infos;

@end

@implementation DDanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initValues];
    [self initViews];
}

- (void) initValues {
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"info" ofType:@"json"]];
    NSError *error;
    self.infos = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}

- (void) initViews {
    self.title = @"广场舞";
    [self.table registerNib:[UINib nibWithNibName:@"DDanceViewCell" bundle:nil] forCellReuseIdentifier:@"customCell"];
}

#pragma mark - 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.infos count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 259;
}
//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DDanceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    
    NSDictionary *info = [self.infos objectAtIndex:indexPath.row];
    cell.banner.image = [UIImage imageNamed:[info objectForKey:@"banner"]];
    cell.info.text = [info objectForKey:@"title"];
    [cell setMultipleTouchEnabled:YES];
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DInfoViewController *dinfo = [[DInfoViewController alloc] initWithNibName:@"DInfoViewController" bundle:nil];
    [dinfo setInfo:[self.infos objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:dinfo animated:YES];
}

@end
